package com.todo.demo.Service.impl;

import com.todo.demo.Dao.PlanAndTaskDao;
import com.todo.demo.Model.*;
import com.todo.demo.Service.LoginService;
import com.todo.demo.Service.PlanAndTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service(value = "planAndTaskService")
public class PlanAndTaskServiceImpl implements PlanAndTaskService {
    @Autowired
    private PlanAndTaskDao planAndTaskDao;//这里会报错，但是并不会影响
    @Autowired
    private LoginService userService;

    /*
     * 这个方法中用到了我们开头配置依赖的分页插件pagehelper
     * 很简单，只需要在service层传入参数，然后将参数传递给一个插件的一个静态方法即可；
     * pageNum 开始页数
     * pageSize 每页显示的数据条数
     * */
    @Override
    public List<HashMap<String, Object>> findAllPlanWithTask(int pageNum, int pageSize, long userId) {
        //将参数传给这个方法就可以实现物理分页了，非常简单。
//        PageHelper.startPage(pageNum, pageSize);

        List<HashMap<String, Object>> list = new ArrayList<>();

        UserIdAndPlanId userIdAndPlanId = new UserIdAndPlanId();
        userIdAndPlanId.setUserId(userId);
        List<Plan> planList = planAndTaskDao.findAllPlan(userIdAndPlanId);

        for (int i = 0; i < planList.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("plan", planList.get(i));

            UserIdAndPlanId userIdAndPlanId1 = new UserIdAndPlanId();
            userIdAndPlanId1.setPlanId(planList.get(i).getPlan_id());
            List<Task> taskList = planAndTaskDao.finTaskByPlanId(userIdAndPlanId1);

            for (int y = 0; y < taskList.size(); y++) {
                User user1 = new User();
                user1.setId(taskList.get(y).getTask_user_id());
                User user2 = userService.getUserById(user1);
                if (user2 != null) {
                    taskList.get(y).setCreator(user2.getNick_name());
                    taskList.get(y).setCreatorImg(user2.getAvatar_url());
                }
            }

            map.put("task", taskList);

            list.add(map);
        }
//            select * from plan  where plan_user_id =  #{userId, jdbcType=INTEGER}

        return list;
    }

    @Override
    public List<PlanWithTask> findPlanWithTaskByUserId(int pageNum, int pageSize, long userId) {
        //将参数传给这个方法就可以实现物理分页了，非常简单。
//        PageHelper.startPage(pageNum, pageSize);
        return planAndTaskDao.findPlanWithTaskByUserId(pageNum, pageSize, userId);
    }

    @Override
    public Plan finPlanByPlanId(long planId) {
        //将参数传给这个方法就可以实现物理分页了，非常简单。
//        PageHelper.startPage(pageNum, pageSize);
        return planAndTaskDao.finPlanByPlanId(planId);
    }

    @Override
    public PlanWithTask finTaskByTaskId(long taskId) {
        return planAndTaskDao.finTaskByTaskId(taskId);
    }

    @Override
    public long addPlan(Plan plan) {
        planAndTaskDao.addPlan(plan);

        return plan.getPlan_id();
    }

    public long addTask(Task task) {
        planAndTaskDao.addTask(task);

        return task.getTask_id();
    }

    public int setTaskCheckById(Task task) {
        return planAndTaskDao.setTaskCheckById(task);
    }

    public int deleteTaskById(PlanWithTask planWithTask) {
        return planAndTaskDao.deleteTaskById(planWithTask);
    }

    public int deletePlanById(PlanWithTask planWithTask) {
        planAndTaskDao.deletePlanByPlanId(planWithTask);
        planAndTaskDao.deleteTaskByPlanId(planWithTask);
        return 1;
    }

    @Override
    public long getTaskAiNum() {
        return planAndTaskDao.getTaskAiNum();
    }

    @Override
    public long getPlanAiNum() {
        return planAndTaskDao.getPlanAiNum();
    }

    @Override
    public int addUserToPlan(UserWithPlan userWithPlan) {
        return planAndTaskDao.addUserToPlan(userWithPlan);
    }

    @Override
    public List<Partner> getPartnerByPlanId(Partner partner) {
        return planAndTaskDao.getPartnerByPlanId(partner);
    }

    @Override
    public Partner isPartakeRepeat(Partner partner) {
        return planAndTaskDao.isPartakeRepeat(partner);
    }

    @Override
    public int updateTypeIdForPLan(Plan plan) {
        return planAndTaskDao.updateTypeIdForPLan(plan);
    }

    public int quitPartake(Partner partner) {
        return planAndTaskDao.quitPartake(partner);
    }

    public int updatePlanLastEditTime(Plan plan) {
        return planAndTaskDao.updatePlanLastEditTime(plan);
    }
}

